package suzf.top.base.widgets

import android.content.Context
import android.util.AttributeSet
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.ashokvarma.bottomnavigation.ShapeBadgeItem
import com.ashokvarma.bottomnavigation.TextBadgeItem
import suzf.top.base.R

/**
 *  底部导航栏
 *========================
 * Created with AndroidStudio
 * Author:Mr.Su
 * Date:2019-09-19
 * Time:09:11
 * ========================
 */
class BottomNavBar @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BottomNavigationBar(context, attrs, defStyleAttr) {

    private val mCartBadge: TextBadgeItem
    private val mMsgBadge: ShapeBadgeItem

    init {
        //  首页
        val homeItem = BottomNavigationItem(R.drawable.btn_nav_home_press,resources.getString(R.string.nav_bar_home))
                //未选中的图标
                .setInactiveIconResource(R.drawable.btn_nav_home_normal)
                //选中的颜色
                .setActiveColorResource(R.color.common_blue)
                //未选中的颜色
                .setInActiveColorResource(R.color.text_normal)

        //  分类
        val categoryItem = BottomNavigationItem(R.drawable.btn_nav_category_press,resources.getString(R.string.nav_bar_category))
                //未选中的图标
                .setInactiveIconResource(R.drawable.btn_nav_category_normal)
                //选中的颜色
                .setActiveColorResource(R.color.common_blue)
                //未选中的颜色
                .setInActiveColorResource(R.color.text_normal)

        //  购物车
        val cartItem = BottomNavigationItem(R.drawable.btn_nav_cart_press,resources.getString(R.string.nav_bar_cart))
                //未选中的图标
                .setInactiveIconResource(R.drawable.btn_nav_cart_normal)
                //选中的颜色
                .setActiveColorResource(R.color.common_blue)
                //未选中的颜色
                .setInActiveColorResource(R.color.text_normal)
        mCartBadge = TextBadgeItem()
        mCartBadge.hide()
        cartItem.setBadgeItem(mCartBadge)

        //  消息
        val msgItem = BottomNavigationItem(R.drawable.btn_nav_msg_press,resources.getString(R.string.nav_bar_msg))
                //未选中的图标
                .setInactiveIconResource(R.drawable.btn_nav_msg_normal)
                //选中的颜色
                .setActiveColorResource(R.color.common_blue)
                //未选中的颜色
                .setInActiveColorResource(R.color.text_normal)
        mMsgBadge = ShapeBadgeItem()
        mMsgBadge.setShape(ShapeBadgeItem.SHAPE_OVAL)
        mMsgBadge.hide()
        msgItem.setBadgeItem(mMsgBadge)

        //  我的
        val userItem = BottomNavigationItem(R.drawable.btn_nav_user_press,resources.getString(R.string.nav_bar_user))
                //未选中的图标
                .setInactiveIconResource(R.drawable.btn_nav_user_normal)
                //选中的颜色
                .setActiveColorResource(R.color.common_blue)
                //未选中的颜色
                .setInActiveColorResource(R.color.text_normal)

        //  设置模式
        setMode(BottomNavigationBar.MODE_FIXED)
        //  设置背景样式
        setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        //  设置背景颜色
        setBarBackgroundColor(R.color.common_white)

        //添加导航栏并初始化
        addItem(homeItem)
                .addItem(categoryItem)
                .addItem(cartItem)
                .addItem(msgItem)
                .addItem(userItem)
                .setFirstSelectedPosition(0)
                .initialise()
    }

    fun checkCartBadge(count: Int) {
        if (count == 0) {
            mCartBadge.hide()
        }else {
            mCartBadge.setText("$count")
            mCartBadge.show()
        }
    }

    fun checkMsgBadge(isVisiable: Boolean) {
        if (isVisiable) {
            mMsgBadge.show()
        }else {
            mMsgBadge.hide()
        }
    }
}